package davidtps.godutch.database.sqlitedal;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import davidtps.godutch.activity.R;
import davidtps.godutch.database.base.SQLiteDALbase;
import davidtps.godutch.model.ModelConsume;
import davidtps.godutch.utility.DateTools;

public class SQLiteDALConsume extends SQLiteDALbase {

	public SQLiteDALConsume(Context pContext) {
		super(pContext);
	}
	
	public boolean insertModelConsume(ModelConsume pModelConsume){
		ContentValues _ContentValues = genarateContentValues(pModelConsume);
		long ConsumeId = getDatabase().insert(getTableNameAndPK()[0], null, _ContentValues);
//		pModelConsume.setConsumeId((int) ConsumeId);
		return ConsumeId != -1;
	}
	public int getConsumeCount(String pCondition){
		return getCount(pCondition);
		
	}
	
//	public Cursor execSQL(String pSqlString){
//		Cursor _Cursor = execSQL(pSqlString);
//		return _Cursor;
//	}
	
	public boolean updateModelConsume(String pCondition,ModelConsume pModelConsume){
		ContentValues _ContentValues = genarateContentValues(pModelConsume);
		return getDatabase().update(getTableNameAndPK()[0], _ContentValues, pCondition, null) >0;
	}
	public boolean updateModelConsume(String pCondition,ContentValues pContentValues){
		return getDatabase().update(getTableNameAndPK()[0], pContentValues, pCondition, null) >0;
	}
	
	public boolean deleteModelConsume(String pCondition){
		return delete(getTableNameAndPK()[0], pCondition);
	}

	public List<ModelConsume> getModelConsumes(String pCondition){
		String _sqlString = "select * from v_consume where 1=1 "+pCondition;
		return getList(_sqlString);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		StringBuilder _Builder = new StringBuilder();
		_Builder.append("Create  TABLE MAIN.[consumes](");
		_Builder.append("[ConsumeId] integer PRIMARY KEY AUTOINCREMENT NOT NULL");
		_Builder.append(",[categoryId] integer NOT NULL");
		_Builder.append(",[userId] varchar(100) NOT NULL");
		_Builder.append(",[accountId] integer NOT NULL");
		_Builder.append(",[consumeState] integer NOT NULL");
		_Builder.append(",[consumeMode] varchar(20) NOT NULL");
		_Builder.append(",[consumeRemark] varchar(200) NOT NULL");
		_Builder.append(",[consumeMoney] numeric NOT NULL DEFAULT 0");
		_Builder.append(",[ConsumeDate] datetime NOT NULL");
		_Builder.append(",[createDate] datetime NOT NULL");
		_Builder.append(");");
		db.execSQL(_Builder.toString());
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	@Override
	protected Object findModel(Cursor pCursor) {
		ModelConsume _ModelConsume = new ModelConsume();
		_ModelConsume.setConsumeId(pCursor.getInt(pCursor.getColumnIndex("ConsumeId")));
		_ModelConsume.setAccountId(pCursor.getInt((pCursor.getColumnIndex("accountId"))));
		_ModelConsume.setAccountName((pCursor.getString(pCursor.getColumnIndex("accountName"))));
		_ModelConsume.setCategoryId(pCursor.getInt((pCursor.getColumnIndex("categoryId"))));
		_ModelConsume.setCategoryName((pCursor.getString(pCursor.getColumnIndex("CategoryName"))));
		_ModelConsume.setCategoryPath((pCursor.getString(pCursor.getColumnIndex("path"))));
		_ModelConsume.setConsumeMoney(new BigDecimal(pCursor.getString(((pCursor.getColumnIndex("consumeMoney"))))));
			Date _ConsumeDate = DateTools.getDate(pCursor.getString(pCursor.getColumnIndex("ConsumeDate")), "yyyy-MM-dd");	
		_ModelConsume.setConsumeDate(_ConsumeDate);
		_ModelConsume.setConsumeMode((pCursor.getString(pCursor.getColumnIndex("consumeMode"))));
		_ModelConsume.setUserId((pCursor.getString(pCursor.getColumnIndex("userId"))));
		_ModelConsume.setConsumeRemark((pCursor.getString(pCursor.getColumnIndex("consumeRemark"))));
			Date _CreateDate = DateTools.getDate(pCursor.getString(pCursor.getColumnIndex("createDate")), "yyyy-MM-dd HH:mm:ss");	
		_ModelConsume.setCreateDate(_CreateDate);
		_ModelConsume.setConsumeState((pCursor.getInt(pCursor.getColumnIndex("consumeState"))));
		
		return _ModelConsume;
	}

	@Override
	protected String[] getTableNameAndPK() {
		String[] tnAndPk = new String[]{"Consumes","ConsumeId"};
		return tnAndPk;
	}
	
	private ContentValues genarateContentValues(ModelConsume pModelConsume){
		ContentValues _ContentValues = new ContentValues();
		_ContentValues.put("accountId", pModelConsume.getAccountId());
		_ContentValues.put("userId", pModelConsume.getUserId());
		_ContentValues.put("categoryId", pModelConsume.getCategoryId());
		_ContentValues.put("consumeMoney", pModelConsume.getConsumeMoney()+"");
		_ContentValues.put("consumeMode", pModelConsume.getConsumeMode());
		_ContentValues.put("consumeRemark", pModelConsume.getComsumeRemark());
		_ContentValues.put("consumedate", DateTools.getFormatDateTime(pModelConsume.getConsumeDate(), "yyyy-MM-dd"));
		_ContentValues.put("consumeState", 1);
		
		if(pModelConsume.getCreateDate()==null){
			_ContentValues.put("createDate", DateTools.getFormatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss"));
		}else{
			_ContentValues.put("createDate", DateTools.getFormatDateTime(pModelConsume.getCreateDate(), "yyyy-MM-dd HH:mm:ss"));
		}
		return _ContentValues;
	}
	
}
